package com.chima.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import com.chima.domain.Post;

public class PostDao extends HibernateDaoSupport{
	//分页查询
			public List<Post> findByCriteria(DetachedCriteria criteria, int firstResult,int maxResults) {
				return this.getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults);
			}
		   
			//查找总页数
			public int findByTotalCount(int depart) {
				
				long temp=231231l;
				if(depart==0){
					List list= this.getHibernateTemplate().find("select count(*) from Post");
					temp = (Long) list.get(0);
				}else{
					List list= this.getHibernateTemplate().find("select count(*) from Post p where p.department.id='"+depart+"' ");
					 temp = (Long) list.get(0);
				}
				return (int) temp;
			}
			public void save(Post post) {
				this.getHibernateTemplate().save(post);
			}
			public void update(Post post) {
				this.getHibernateTemplate().update(post);
			}
			
			public void delete(int id) {
				this.getHibernateTemplate().delete(this.getHibernateTemplate().load(Post.class, id));
			}
			public List<Post> findPostByDept(int deptID){
				Session session   =this.getSession();
				List<Post> p = (List<Post>)session.createQuery("from Post p where p.department.id = ?").setParameter(0, deptID).list();
				super.releaseSession(session);
				return p;
				
			}

}
